<?
session_start();
include_once("includes/common_public_inc.php");
$cur_grade = 0;
$user_id = getParamInt("user_id","");
$grade = getParam("grade","");
$submit = getParam("submit","");

$tplRate=new TplLoad;
$tplRate->assign("phpself",$_SERVER['PHP_SELF']);

if ($set_rate_mem_require_member==1)
	check_valid_user("popup");
	
$sql = "select user_name,user_votes,user_voters,user_voters_ip from $usr_tbl where user_id = $user_id";
$res = q($sql);
$row = mysql_fetch_array($res);
$name = $row["user_name"];
$votes = $row["user_votes"];
$voters = $row["user_voters"];
$voters_ip = $row["user_voters_ip"];

if ($votes and $voters)
	$cur_grade = round($votes / $voters, 1);

if (isset($submit) AND $grade)
{
	$voters_ip_current = $_SERVER["REMOTE_ADDR"]; 
	if ($voters_ip_current == $voters_ip AND $voters_ip!=null)
		$tplRate->assign("rate_fail",1);
	else if ($user_id==$_SESSION["valid_user"])
		$tplRate->assign("rate_fail",2);
	else 
	{
		$votes = $votes + $grade;
		
		if (!$voters)
		{
			$sql = "update $usr_tbl set user_voters = 0,user_votes=0 where user_id = $user_id";	
			$r = q($sql);
		}
		$new_grade = round($votes / ($voters+1), 1);
		$sql = "update $usr_tbl set user_voters=user_voters + 1, user_votes=$votes,user_score=$new_grade,user_voters_ip='$voters_ip_current' where user_id = $user_id";
		$res = q($sql);
		$tplRate->assign("rate_ok",1);
		addToHistory(9,$user_id,"","");
	}

	
}
else
{

	$sql = "select user_name,user_score from $usr_tbl where user_id = $user_id ";
	$res_2 = q($sql);
	$row = mysql_fetch_array($res_2);
	$ad_owner = $row["ad_owner"];
	$name = $row["user_name"];
	$userscore = $row["user_score"];

	$tplRate->assign("ad_rate_info",formatString(LA_RATE_PERSON,array($name))); 
	$tplRate->assign("cur_grade",$cur_grade);	
	$tplRate->assign("user_id",$user_id);	
	if (!$voters)
		$voters=0;
	$tplRate->assign("voters",$voters);	
	
}
$tplRate->assign("charset",$set_charset);
$tplRate->display("rate_member.tpl");
?>

         

